import json

from django.http import HttpResponse
from django.shortcuts import render
from django.views import View


class UrlView(View):
    def get(self, request, mobile):

        print(mobile)

        return HttpResponse('解析路径传参--正则')

class ParseArgumentsView(View):
    def post(self, request):
        # get 没有请求 体

        ############################request.POST########################
        # 解析表单参数---request.POST ==>QueryDict: 一键多值
        # data = request.POST
        # print(data)
        # print(data.get('name'))
        # print(data.getlist('age'))
        #
        # return HttpResponse('解析表单参数---request.POST')

        ############################request.body########################
        # 解析非表单Json参数---request.body ==>Bytes
        # json_bytes = request.body
        # json_dict = json.loads(json_bytes)
        # print(json_bytes)
        # print(json_dict)
        # print(type(json_dict))

        """
        json模块-- 将python的list/dict <--> json字符串
        
        dumps ---> 将dict/list --> json字符串
        loads ---> 将json字符串/bytes --> dict/list
        
        """

        # return HttpResponse('解析非表单Json参数---request.body')

        ############################request.Meta########################

        headers = request.META
        print(headers.get('date', 'Key不存在'))
        print(headers['HTTP_DATE'])
        return HttpResponse('解析请求头参数---request.META')


    def get(self, request):

        # 1. 解析查询参数 ---request.GET ==>queryDict 允许一键多值
        params = request.GET

        print(params)
        print(params.get('b', '默认值'))
        print(params.getlist('a'))
        print(params.get('c', '默认值'))

        return HttpResponse('解析查询参数 ---request.GET!')